<template>
  <el-dialog
    :visible.sync="isShow"
    :title="{ add: '新增权限', edit: '编辑权限' }[mode]"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item prop="enVisible" label="企业可见">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission.js'
export default {
  name: 'DialogItem',
  data() {
    return {
      isShow: false,
      mode: 'add',
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        code: [{ required: true, message: '必填', trigger: 'blur' }],
        description: [{ required: true, message: '必填', trigger: 'blur' }],
        enVisible: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 关闭弹框
    close() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.mode = 'add'
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      }
    },
    // 确定点击
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
          } else {
            await sysPermissionPut(this.form)
          }
          this.$message.success(this.mode === 'add' ? '新增成功' : '编辑成功')
          this.close()
          this.$emit('getPermissions')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-input {
    width: 700px;
  }
}
</style>
